package logic.moves;

import java.io.Serializable;

import logic.*;
import logmngr.*;

/**
 * 
 * @author Paul
 * Move simulates settling in uninhabited land by player
 * Done at beginning of game, splitting the map among players
 */
public class SettleMove implements GameMove, Serializable  {

	private static final long serialVersionUID = 1L;

	//=== Fields ===
	public static final String move_type = "SETTLEMOVE";
	
	private 	Country 	country;
	private		Player		settler;
	private		boolean		moved;

	//=== Constructors ===
	public SettleMove(Country country, Player settler) {
		moved = false;
		this.country = country;
		this.settler = settler;
	}

	//=== Methods ===country2
	public boolean makeMove() {
		if(!this.hasMoved())
		{
			if(!country.hasOwner() && !settler.hasCountry(country))
			{
				settler.addCountry(country);
				country.setOwner(settler);
				settler.placeSoldiers(country, 1);
				moved=true;
				return true;
			}
			else if(country.hasOwner()){
				LogMngr.getLogger().writeLog(LogType.Err, "Trying to settle in already settled country");
				return false;
			}
			else if(settler.hasCountry(country)){
				LogMngr.getLogger().writeLog(LogType.Err, "Trying to add already added country into settler");
				return false;
			}
		}
		return false;
	}

	public void restore() {
		if(hasMoved())
		{
			settler.removeCountry(country);
			country.setOwner(null);
			settler.unplaceSoldier(country);
			moved = false;
		}
	}

	public boolean hasMoved() {
		return moved;
	}
	
	public Country getCountry(){
		return country;
	}
	
	public Player getSettler(){
		return settler;
	}
	
	/**
	 * print a description of the move to the STD out
	 */
	public void printDoMove(){
		System.out.println(this.settler.getPlayerName() + " has settled in " + this.country); 
	}
	
	/**
	 * print a description of the undoing of the move to the STD out
	 */
	public void printUndoMove(){
		System.out.println(this.settler.getPlayerName() + " un-settled in " + this.country);
	}

	/**
	 * @return move type as string
	 */
	public String getType() {
		 return move_type;
	}

	public void printShortDescription() {
		System.out.println(this.settler.getPlayerName() + " has settled in " + this.country); 
		
	}

	public String getBriefDo() {
		return this.settler.getPlayerName() + " has settled in " + this.country;
	}

	public String getBriefUndo() {
		return this.settler.getPlayerName() + " un-settled in " + this.country;
	}
}
